#include<bits/stdc++.h>
#define int long long
#define ls(x) ((x)*2)
#define rs(x) ((x)*2+1)
#define Debug(...) fprintf(stderr, __VA_ARGS__)
#define For(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define Rof(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define rep(i,  b) for(int i=1,i##end=b;i<=i##end;i++)
using namespace std;
const int N=1e6+5,base=999983,Mod=998244353;
//char buf[(1<<21)+5],*p1,*p2;
//#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
inline void chmx(int &x,int y){(x<y)&&(x=y);}
inline void chmn(int &x,int y){(x>y)&&(x=y);}
inline void Add(int &x,int y){(x=x+y+Mod)%=Mod;}
inline int read(){
	int f=0,x=0;
	char ch=getchar();
	while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}
	while(isdigit(ch)){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
	return f?-x:x;
}
void print(int n){
    if(n<0){
        putchar('-');
        n*=-1;
    }
    if(n>9) print(n/10);
    putchar(n%10+'0');
}
int n,m;
struct Tree{
	vector<int>q[N];
	int fa[N][21];
	int dep[N],siz[N];
	void dfs(int x){
		dep[x]=dep[fa[x][0]]+1;
		siz[x]=1;
		for(auto y:q[x]){
			dfs(y);
			siz[x]+=siz[y];
		}
	}
	inline int LCA(int x,int y){
		if(x==y) return x;
		if(dep[x]<dep[y]) swap(x,y);
		Rof(i,20,0){
			if(dep[fa[x][i]]>=dep[y]){
				x=fa[x][i];
			}
		}
		if(x==y) return x;
		Rof(i,20,0){
			if(fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i];
		}
		return fa[x][0];
	}
	inline int find(int x,int y){
		if(x==y) return x;
		if(dep[x]<dep[y]) swap(x,y);
		Rof(i,20,0){
			if(dep[fa[x][i]]>dep[y]){
				x=fa[x][i];
			}
		}
		return x;
		
	}
	inline void build(){
		For(i,1,n) if(fa[i][0])q[fa[i][0]].push_back(i);
		For(i,1,20){
			For(j,1,n){
				fa[j][i]=fa[fa[j][i-1]][i-1];
			}
		}
		int rt=0;
		For(i,1,n) {
			if(!fa[i][0]){
				rt=i;
			}
		}
		dfs(rt);
	}
}tr1,tr2; 
int a[N];
inline int path(int x,int y){
	int lca=tr1.LCA(x,y);
	if(lca==y) swap(x,y);
//	cout<<x<<" "<<y<<" "<<tr1.siz[x]<<" "<<tr1.siz[y]<<" "<<lca<<endl;
	if(lca==x){
		return tr1.siz[y]*(n-tr1.siz[tr1.find(x,y)]);
	}
	else return tr1.siz[x]*tr1.siz[y];
}
signed main(){
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	// ios::sync_with_stdio(false);
	// cin.tie(0); cout.tie(0);
	n=read(),m=read();
	For(i,1,n) tr1.fa[i][0]=read();
	For(i,1,n) tr2.fa[i][0]=read();
	For(i,1,n) a[i]=read();
	tr1.build(),tr2.build(); 
	int ans=0;
	For(i,1,n){
		For(j,i+1,n){
			ans+=path(i,j)*tr2.dep[tr2.LCA(a[i],a[j])]%Mod;
			ans%=Mod;
//			cout<<i<<" "<<j<<" "<<path(i,j)<<endl;
		}
	} 
	printf("%lld\n",ans);
#ifdef LOCAL
    Debug("\nMy Time: %.3lfms\n", (double)clock() / CLOCKS_PER_SEC);
#endif
	return 0;
}

